package com.alstudio.utils.android.view.abslistview;

import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.alstudio.utils.image.ALImageWorkerManager;
import com.alstudio.utils.log.Mylog;

public class ALAbsListViewScrollHandler implements AbsListView.OnScrollListener {

	private final String TAG = "ALAbsListViewScrollHandler";
	private BaseAdapter mAdapter;
	private ALImageWorkerManager mImageWorker;
	private boolean isFling = false;
	private final int TOP_POSITON = 0;
	private ALAbsListViewLoadMoreEngine mLoadMoreEngin;

	private ALAbsListViewPositionChangeListener mListener;

	private AbsListViewScrollState mLastState = AbsListViewScrollState.SCROLL_STATE_IDLE;

	private boolean isAutoFlushAdapter = true;

	public ALAbsListViewScrollHandler(BaseAdapter adapter,
			ALImageWorkerManager imageWoker,
			ALAbsListViewPositionChangeListener listener) {
		// TODO Auto-generated constructor stub
		if (adapter == null) {
			throw new IllegalArgumentException("我不要空的适配器！");
		}
		mAdapter = adapter;
		this.mImageWorker = imageWoker;
		this.mListener = listener;
	}

	public ALAbsListViewScrollHandler(BaseAdapter adapter,
			ALImageWorkerManager imageWoker,
			ALAbsListViewPositionChangeListener listener,
			ALAbsListViewLoadMoreEngine loadMoreEngine) {
		this(adapter, imageWoker, listener);
		this.mLoadMoreEngin = loadMoreEngine;
	}

	public BaseAdapter getAdapter() {
		return mAdapter;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.mAdapter = adapter;
	}

	public ALImageWorkerManager getImageWorker() {
		return mImageWorker;
	}

	public ALAbsListViewPositionChangeListener getListener() {
		return mListener;
	}

	public void setmListener(ALAbsListViewPositionChangeListener listener) {
		this.mListener = listener;
	}

	public void setImageWorker(ALImageWorkerManager imageWorker) {
		this.mImageWorker = imageWorker;
	}

	public void setmLoadMoreEngin(ALAbsListViewLoadMoreEngine loadMoreEngin) {
		this.mLoadMoreEngin = loadMoreEngin;
	}

	public void setLastState(AbsListViewScrollState lastState) {
		this.mLastState = lastState;
	}

	public AbsListViewScrollState getLastState() {
		return mLastState;
	}

	public void setAutoFlushAdapter(boolean isAutoFlushAdapter) {
		this.isAutoFlushAdapter = isAutoFlushAdapter;
	}

	public boolean isAutoFlushAdapter() {
		return isAutoFlushAdapter;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub

		if (mAdapter.getCount() == 0) {
			Mylog.d("当前适配器中还没有数据");
			return;
		}
		Mylog.d("数据总量: " + totalItemCount + "第一个可见的: " + firstVisibleItem
				+ "总的可见: " + visibleItemCount);

		int lastInScreen = firstVisibleItem + visibleItemCount;

		boolean loadMore = (lastInScreen == totalItemCount);
		if (loadMore) {
			// 已经到底部，回调通知
			if (mListener != null) {
				mListener.onScrollToBottom(view, firstVisibleItem,
						visibleItemCount, totalItemCount);
			}

			if (mLoadMoreEngin != null) {
				Mylog.d("开始加载更多...");
				mLoadMoreEngin.loadMore();
			}
		}

		if (firstVisibleItem == TOP_POSITON) {
			// 已经到顶部，回调通知
			if (mListener != null) {
				mListener.onScrollToTop(view, firstVisibleItem,
						visibleItemCount, totalItemCount);
			}
		}

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
			Mylog.e("正在滑动...");
			isFling = true;
			// if (mImageWorker != null)
			// mImageWorker.getImageLoader().setPauseWork(true);

		} else {
			// if (mImageWorker != null) {
			// mImageWorker.getImageLoader().setPauseWork(false);
			// }
			Mylog.e("停止滑动...");
			if (isFling) {
				isFling = false;
				if (isAutoFlushAdapter) {
					mAdapter.notifyDataSetChanged();
				}
			}
		}
		notfiyScrollStateChanged(view, mListener, scrollState);
	}

	private void notfiyScrollStateChanged(AbsListView view,
			ALAbsListViewPositionChangeListener listener, int scrollState) {
		if (listener == null) {
			return;
		}
		switch (scrollState) {
		case SCROLL_STATE_FLING:
			listener.onScrollFling(view, mLastState);
			break;
		case SCROLL_STATE_IDLE:
			listener.onScrollIdle(view, mLastState);
			break;
		case SCROLL_STATE_TOUCH_SCROLL:
			listener.onTouchScroll(view, mLastState);
			break;
		}

		mLastState = getScrollState(scrollState);
	}

	private AbsListViewScrollState getScrollState(int scrollStateSource) {
		switch (scrollStateSource) {
		case SCROLL_STATE_FLING:
			return AbsListViewScrollState.SCROLL_STATE_FLING;
		case SCROLL_STATE_IDLE:
			return AbsListViewScrollState.SCROLL_STATE_IDLE;
		case SCROLL_STATE_TOUCH_SCROLL:
			return AbsListViewScrollState.SCROLL_STATE_TOUCH_SCROLL;
		}
		return AbsListViewScrollState.SCROLL_STATE_IDLE;
	}

	public enum AbsListViewScrollState {
		SCROLL_STATE_IDLE, SCROLL_STATE_FLING, SCROLL_STATE_TOUCH_SCROLL,
	}
}
